package org.amphiption.gametimer.screen.component;

import org.amphiption.gametimer.entity.Player;
import org.amphiption.gametimer.util.StringUtil;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;

import com.amphiprion.prototype.ScreenProperty;
import com.amphiprion.prototype.component.AreaComponent;
import com.amphiprion.prototype.util.BitmapUtil;
import com.amphiprion.prototype.util.NormalizedBitmap;

public class ActivePlayerComponent extends AreaComponent {
	private Player player;
	private Paint paintText;
	private Paint paintTime;
	private boolean turnMode;
	private NormalizedBitmap panel;
	private String uri;
	private Paint paint;

	public ActivePlayerComponent(Player player, float x, float y, float width, float height, int alpha, boolean turnMode) {
		super(player.getColor(), x, y, width, height, alpha);
		this.turnMode = turnMode;
		uri = "active_panel.png";
		paint = new Paint();

		paintText = new Paint();
		paintText.setTextSize(32.0f);
		paintText.setColor(Color.BLACK);
		paintText.setAntiAlias(true);

		paintTime = new Paint();
		paintTime.setColor(Color.WHITE);
		paintTime.setTextSize(80.0f);
		paintTime.setAntiAlias(true);
	}

	public void setPlayer(Player player) {
		this.player = player;
		// paintText.setColor(-player.getColor());
		setColor(player.getColor());

	}

	public Player getPlayer() {
		return player;
	}

	@Override
	public void onDraw(Canvas canvas, ScreenProperty screenProperty) {
		// super.onDraw(canvas, screenProperty);
		float left = x - width * scale / 2;
		float top = y - height * scale / 2;
		float right = x + width * scale / 2;
		float bottom = y - height * scale / 2 + 100;
		drawFillRect(canvas, left, top, right, bottom, screenProperty);
		drawImage(canvas, panel.bitmap, (left + right) / 2, (top + bottom) / 2, scale, screenProperty);
		drawText(canvas, player.getName(), x, y - 145, 1.0f, screenProperty, paintText);
		if (turnMode) {
			drawText(canvas, "" + player.getCounter(), x, y, 1.0f, screenProperty, paintTime);
		} else {
			drawText(canvas, StringUtil.durationToString(player.getCounter()), x, y, 1.0f, screenProperty, paintTime);
		}
	}

	protected void drawImage(Canvas canvas, Bitmap img, float centerX, float centerY, float scale, ScreenProperty screenProperty) {
		if (alpha == 0 || img == null) {
			return;
		}
		// paint.setAlpha(alpha);
		Matrix m = new Matrix();
		m.postTranslate(-img.getWidth() / 2, -img.getHeight() / 2);
		m.postRotate(screenProperty.screenRotation + rotation);
		m.postScale(scale * screenProperty.screenScale, scale * screenProperty.screenScale);

		if (screenProperty.screenRotation == 0) {
			m.postTranslate(centerX * screenProperty.screenScale + screenProperty.screenOffsetX, centerY * screenProperty.screenScale + screenProperty.screenOffsetY);
		} else {
			m.postTranslate(canvas.getWidth() - centerY * screenProperty.screenScale + screenProperty.screenOffsetX, centerX * screenProperty.screenScale
					+ screenProperty.screenOffsetY);
		}
		canvas.drawBitmap(img, m, paint);
	}

	private void drawText(Canvas canvas, String str, float centerX, float centerY, float scale, ScreenProperty screenProperty, Paint paint) {

		paint.setAlpha(alpha);
		float pSize = paint.getTextSize();
		// if ("center".equals(alignment)) {
		centerX -= scale * paint.measureText(str) / 2;
		// }
		paint.setTextSize(pSize * screenProperty.screenScale * scale);
		Matrix m = new Matrix();
		// m.postTranslate(-canvas.getWidth() / 2, -canvas.getHeight() / 2);
		m.postRotate(screenProperty.screenRotation);
		if (screenProperty.screenRotation == 0) {
			// m.postTranslate(canvas.getWidth() / 2, canvas.getHeight() / 2);
		} else {
			m.postTranslate(canvas.getWidth(), 0);
		}
		float txtX;
		float txtY;
		txtX = centerX * screenProperty.screenScale;
		txtY = centerY * screenProperty.screenScale;
		txtY += pSize * 0.3 * screenProperty.screenScale * scale;

		canvas.save();
		canvas.setMatrix(m);
		canvas.drawText(str, txtX - screenProperty.screenOffsetX, txtY - screenProperty.screenOffsetY, paint);
		// canvas.drawRect(0, 0, 400, 240, paint);
		canvas.restore();
		paint.setTextSize(pSize);

	}

	@Override
	public void activate() {
		panel = BitmapUtil.loadBitmap(uri);
		super.activate();
	}

	@Override
	public void desactivate() {
		super.desactivate();
		BitmapUtil.unloadBitmap(uri);
	}
}
